import java.io.*;

public class SerwerListener extends Thread
{
	private String Message;
	private BufferedReader in;
	private int serverStatus=1, iTempTable[], iTemp[];
	private String[] tableOfInformations, sTempTable, sDoubleTempTab[];
	BadugiClient bc;
	SerwerListener(BufferedReader in, BadugiClient bc)
	{
		this.in=in;
		this.bc=bc;
	}
	public void run()
	{
		while(serverStatus==1)
		{
			try
			{
				Message=in.readLine();
				
			}
			catch (IOException b)
			{
				System.out.println("Serwer padl!");
				serverStatus=0;
				System.exit(0);
			}
			tableOfInformations=Message.split(":");
			if(tableOfInformations[0].equals("S"))
			{
				try
				{
					iTemp = new int[5];
					iTemp[0]=Integer.parseInt(tableOfInformations[1]);
					iTemp[1]=Integer.parseInt(tableOfInformations[2]);
					iTemp[2]=Integer.parseInt(tableOfInformations[3]);
					iTemp[3]=Integer.parseInt(tableOfInformations[4]);
					iTemp[4]=Integer.parseInt(tableOfInformations[5]);
					iTempTable = new int[iTemp[0]];
					sTempTable = new String[iTemp[0]];
					for(int i=0; i<iTemp[0]; i++)
					{
						iTempTable[i]=Integer.parseInt(tableOfInformations[6+i]);
						sTempTable[i]=tableOfInformations[6+iTemp[0]+i];
					}
					bc.addMainPanel(iTemp[0], iTemp[1], iTempTable, sTempTable);
					bc.allNewDeal(iTemp[3], ((iTemp[3]+1)%iTemp[0]), ((iTemp[3]+2)%iTemp[0]));
					bc.iNewDeal(iTemp[1], iTemp[4], iTemp[2]);
					bc.setVisible(true);
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
			}
			else if(tableOfInformations[0].equals("AM"))
			{
				try
				{
					bc.allMove(Integer.parseInt(tableOfInformations[1]), tableOfInformations[2], tableOfInformations[3], tableOfInformations[4]);
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
				bc.setVisible(true);
			}
			else if(tableOfInformations[0].equals("ACT"))
			{
				bc.allChangeTime();
				bc.setVisible(true);
			}
			else if(tableOfInformations[0].equals("AC"))
			{
				try
				{
					iTempTable = new int[4];
					iTempTable[0]=Integer.parseInt(tableOfInformations[2].substring(0,1));
					iTempTable[1]=Integer.parseInt(tableOfInformations[2].substring(1,2));
					iTempTable[2]=Integer.parseInt(tableOfInformations[2].substring(2,3));
					iTempTable[3]=Integer.parseInt(tableOfInformations[2].substring(3,4));
					bc.allChange(Integer.parseInt(tableOfInformations[1]), iTempTable);
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
				bc.setVisible(true);
			}
			else if(tableOfInformations[0].equals("AED"))
			{
				try
				{
					iTemp = new int[2];
					iTemp[0]=Integer.parseInt(tableOfInformations[1]);
					iTemp[1] = (tableOfInformations.length-2)/5;
					iTempTable = new int[iTemp[1]];
					sDoubleTempTab = new String[iTemp[1]][];
					for(int i=0; i<iTemp[1]; i++)
					{
						iTempTable[i]=Integer.parseInt(tableOfInformations[i*5+2]);
						sDoubleTempTab[i] = new String[4];
						for(int j=0; j<4; j++)
						{
							sDoubleTempTab[i][j]=tableOfInformations[i*5+3+j];
						}
					}
					bc.allEndDeal(iTemp[0], iTempTable, sDoubleTempTab);
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
				bc.setVisible(true);
			}
			else if(tableOfInformations[0].equals("AND"))
			{
				try
				{
					bc.allNewDeal(Integer.parseInt(tableOfInformations[1]), Integer.parseInt(tableOfInformations[2]), Integer.parseInt(tableOfInformations[3]));
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
			}
			else if(tableOfInformations[0].equals("IM"))
			{
				try
				{
					iTemp = new int[2];
					iTemp[0]=Integer.parseInt(tableOfInformations[1]);
					iTemp[1]=Integer.parseInt(tableOfInformations[2]);
					bc.iMove(iTemp[0], iTemp[1]);
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
				bc.setVisible(true);
			}
			else if(tableOfInformations[0].equals("INC"))
			{
				sTempTable = new String[4];
				for(int i=0; i<4; i++)
				{
					sTempTable[i]=tableOfInformations[i+1];
				}
				bc.iNewCards(sTempTable);
			}
			else if(tableOfInformations[0].equals("IND"))
			{
				try
				{
					if(tableOfInformations.length==4)
					{
						bc.iNewDeal(Integer.parseInt(tableOfInformations[1]), Integer.parseInt(tableOfInformations[2]), Integer.parseInt(tableOfInformations[3]));
					}
					else
					{
						bc.iNewDeal(Integer.parseInt(tableOfInformations[1]), Integer.parseInt(tableOfInformations[2]));
					}
				}
				catch(NumberFormatException ex)
				{
					System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
				}
			}
		}
	}
}
